import { Component, OnInit, Input } from '@angular/core';
import { AppGlobalService } from '../../shared/service/app-global.service';
import { HttpService } from '../../shared/service/http.service';
import { MessageService } from "src/app/shared/service/message.service";
import { Subscription } from "rxjs";
@Component({
  selector: 'app-mine-list',
  templateUrl: './mine-list.component.html',
  styleUrls: ['./mine-list.component.scss'],
})
export class MineListComponent implements OnInit {
  @Input() tagData: any;
  pageNo: any = 1;
  params: any;
  sessionId = localStorage.getItem('sessionId');
  // 声明订阅对象
  subscription: Subscription = null;
  constructor(
    private appGlobalService: AppGlobalService,
    private message: MessageService,
    private http: HttpService) {
  }
  ngOnInit() {
    this.params = this.tagData.data.submit;
    if (this.appGlobalService.needAutoChoosenTab) {
      if (this.appGlobalService.globalSubscription && this.appGlobalService.globalSubscription.closed === false) {
        this.appGlobalService.globalSubscription.unsubscribe();
      }
    }
    if (!this.subscription && this.tagData.data.submit !== undefined) {
      this.message.clearMessage();
      // 获取筛选参数
      this.subscription = this.message.getMessage().subscribe(param => {
        if (param === undefined) {
          return;
        } else if (param.type === 'ionRefresh') {
          // this.refreshData(param);
          return;
        }
        this.pageNo = 1;
        this.params.sessionId = localStorage.getItem("sessionId");
        // 参数
        this.params.content = param;
        // 页面数据参数
        this.params.content.pageNo = this.pageNo;
        // 请求获取新数据
        this.http.post(
          "application/submit",
          this.params,
          res => {
            console.log("submit");
            // 请求数据成功
            if (res.success) {
              // 切换submit返回数据
              if (Array.isArray(res.data.data) && res.data.data.length > 0) {
                // // 追加数据
                this.tagData.data.data = res.data.data;
                // res.data.data.map(item => {
                //   this.tagData.data.data.push(item);
                // });
                this.params = res.data.submit;
              } else {
                this.tagData.data.data = res.data.data;
                // 没有数据了
                // this.appGlobalService.showToast('没有数据了...', 1000, 'bottom');
              }
              // event.target.complete();
            } else {
              // 请求数据失败
              this.appGlobalService.showToast(res.message, 2000, "middle");
            }
          },
          err => {
            // 请求数据失败
            this.appGlobalService.showToast(err.message, 2000, "middle");
          }
        );
      });
      // tslint:disable-next-line:no-string-literal
      this.subscription['uuid'] = '' + new Date().getTime();
      // tslint:disable-next-line:no-string-literal
      console.log('MineList-' + this.subscription['uuid']);
      this.appGlobalService.tab5Subscription = this.subscription;
    }
  }
  clickTitle() { }
  dismiss() { this.appGlobalService.goBack(); }
  jumpToPage(item) {
    const pageParams = {
      pageId: item.link.target,
      params: item.link.params,
      sessionId: localStorage.getItem('sessionId')
    };
    this.appGlobalService.goToForward(pageParams);
  }
  opeateItem(item) {
    if (item.action === 'link') {
      this.jumpToPage(item);
    } else {
      const params = {
        content: item.submit.content,
        id: item.submit.id,
        sessionId: localStorage.getItem('sessionId')
      };
      this.http.post('application/submit', params, (res: { data: any; success: any; message: string; }) => {
        this.appGlobalService.showToast(res.message, 3000, 'top');
      });
    }
  }

  loadData(event) {
    setTimeout(() => {
      this.params.sessionId = this.sessionId;
      // 请求下一个页面
      // this.pageNo = this.pageNo + 1;
      // 请求获取新数据
      this.http.post('application/submit', this.params, res => {
        // 请求数据成功
        if (res.success) {
          // 追加返回数据
          if (Array.isArray(res.data.data) && res.data.data.length > 0) {
            // 追加数据
            res.data.data.map(item => {
              this.tagData.data.data.push(item);
            });
            this.params = res.data.submit;
          } else {
            // 没有数据了
            // this.appGlobalService.showToast('没有数据了...', 1000, 'bottom');
          }
          event.target.complete();
        } else {
          // 请求失败，更新当前页数
          this.pageNo = this.pageNo - 1;
          // 请求数据失败
          this.appGlobalService.showToast(res.message, 2000, 'middle');
          event.target.complete();
        }
      });
    }, 500);
  }
  doRefresh(event) {
    document.getElementById('refre_sec').style.marginTop = "65px";
    setTimeout(() => {
      this.params.sessionId = this.sessionId;
      this.params.content.pageNo = 1;
      this.http.post('application/submit', this.params, res => {
        if (res.success) {
          this.tagData.data.data = res.data.data;
          this.params.content.pageNo = 2;
          event.target.complete();
          document.getElementById('refre_sec').style.marginTop = "0px";
        } else {
          this.pageNo = 1;
          this.appGlobalService.showToast(res.message, 2000, 'middle');
          event.target.complete();
          document.getElementById('refre_sec').style.marginTop = "0px";
        }
      });
    }, 900);
  }
  // tslint:disable-next-line:use-life-cycle-interface
  ngOnDestroy(): void {
    if (this.subscription) {
      this.subscription.unsubscribe();
      this.appGlobalService.tab5Subscription = this.subscription;
      // tslint:disable-next-line:no-string-literal
      console.log('mineListD' + this.subscription['uuid']);
    }
  }

}
